﻿using Dt261.db;
using Dt261.Model;
using GalaSoft.MvvmLight.Command;
using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Dt261.ViewModel
{
    class ChangePswViewModel
    {
        private string cur;
        private string psw;
        private string repsw;

        public string Psw { get => psw; set => psw = value; }
        public string Repsw { get => repsw; set => repsw = value; }

        public RelayCommand CancelCommand { get; set; }
        public RelayCommand SaveCommand { get; set; }

        public string IsNotSuperUser
        {

            get
            {
                return GlobalData.IsSuperUser()==false ? "Visible" : "Hidden";
            }
        }

        public string Cur { get => cur; set => cur = value; }

        public ChangePswViewModel()
        {

            CancelCommand = new RelayCommand(cancel);
                ;
            SaveCommand = new RelayCommand(save); ;
        }


        void save()
        {
            if(!GlobalData.IsSuperUser())
            {
                if(Cur.IsNullOrEmpty())
                {
                    var result = MessageBoxX.Show("请输入当前密码", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new Panuon.UI.Silver.Core.MessageBoxXConfigurations()
                    {
                        MessageBoxStyle = MessageBoxStyle.Modern,
                        MessageBoxIcon = MessageBoxIcon.Warning,
                        ButtonBrush = "#F1C825".ToColor().ToBrush(),
                    });

                    return;
                }
                else if(!Cur.Equals(GlobalData.LoginUser.Password))
                {
                    var result = MessageBoxX.Show("当前密码错误！", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new Panuon.UI.Silver.Core.MessageBoxXConfigurations()
                    {
                        MessageBoxStyle = MessageBoxStyle.Modern,
                        MessageBoxIcon = MessageBoxIcon.Warning,
                        ButtonBrush = "#F1C825".ToColor().ToBrush(),
                    });

                    return;
                }
            }
            if (Psw.IsNullOrEmpty() || Repsw.IsNullOrEmpty())
            {
                var result = MessageBoxX.Show("请填写密码", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new Panuon.UI.Silver.Core.MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                return;
            }
            else if(!Psw.Equals(Repsw))
            {
                var result = MessageBoxX.Show("两次次密码不一致", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new Panuon.UI.Silver.Core.MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                return;
            }
            else
            {
                UserDb userDb = new UserDb(dbconfig.DatabasePath);

                User user = userDb.GetOneUserByName("普通用户");

                if(user != null)
                {
                    user.Password = Psw;
                    userDb.UpdateUser(user);

                    Notice.Show("修改成功.", "提示", 3, MessageBoxIcon.Success);
                    DMSkin.Core.Broadcast.PushBroadcast("NavigationBack", "ChangePswViewModel");
                }
               
            }
        }

        void cancel()
        {
            //IsSuperUser
            DMSkin.Core.Broadcast.PushBroadcast("NavigationBack", "ChangePswViewModel");
        }
    }
}
